package UserThread;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import UserClientSocket.Client;
import UserGlobal.GlobalData;

public class ChatMesSendThread extends Thread {
    String mes;
    Runnable mAction = null;
    Activity mActivity = null;
    public Boolean isRunning = false;//是否正在运行
    int count = 0;

    public ChatMesSendThread(String c) {
        mes = c;
    }

    public ChatMesSendThread(Activity a, Runnable b, String c) {
        mAction = b;
        mActivity = a;
        mes = c;
    }
    public ChatMesSendThread() {

    }
    @Override
    public void run() {

                isRunning = true;
                try {
                    Client client1 = new Client(5000, 0, 0);
                    client1.sendString(GlobalData.Friendid);//接收人
                    client1.sendString(GlobalData.Myid);//发送人
                    client1.sendString(mes);//消息
                    Map<Integer, String> map = new HashMap<>();
                    map.put(Gravity.RIGHT, mes);
                    ArrayList<Map<Integer, String>> mapslist = new ArrayList<>();
                    int index = GlobalData.mHistoryId.indexOf(GlobalData.Friendid);
                    if (index == -1) {
                        GlobalData.mHistoryId.add(GlobalData.Friendid);
                        GlobalData.mChatHistory.add(mapslist);
                    }
                    index = GlobalData.mHistoryId.indexOf(GlobalData.Friendid);
                    GlobalData.mChatHistory.get(index).add(map);
                    count++;
                    Log.d("socketsend", String.valueOf(count));
                    mActivity.runOnUiThread(mAction);
                    isRunning = false;
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
}